package progress.exa27_3;

import java.util.List;
import java.util.Vector;

public class VectorStudy {
    public static void main(String[] args) {
        List<Integer> list = addTest(100000);
        System.out.println("修改耗时："+updateTest(list)+"ms");
        System.out.println("查询耗时： "+queryTest(list)+"ms");
        System.out.println("插入耗时： "+insertTest(list)+"ms");
        System.out.println("删除耗时： "+deleteTest(list)+"ms");
    }

    /**
     * 添加效率测试
     * @param num
     * @return
     */
    public static List<Integer> addTest(int num){
        long start = System.currentTimeMillis();
        List<Integer> list = new Vector<>();
        for(int i=0;i<num;i++){
            list.add(i);
        }
        System.out.println("添加耗时："+ (System.currentTimeMillis()-start)+ "ms");
        return list;
    }

    /**
     * 更新效率测试
     * @param list
     * @return
     */
    public static long updateTest(List<Integer> list){
        long start = System.currentTimeMillis();
        for(int i=0;i<list.size();i++){
            list.set(i,i+1);
        }
        return System.currentTimeMillis()-start;
    }

    /**
     * 查询效率测试
     * @param list
     * @return
     */
    public static long queryTest(List<Integer> list){
        long start = System.currentTimeMillis();
        for(int i=0;i<list.size();i++){
            list.get(i);
        }
        return System.currentTimeMillis()-start;
    }

    /**
     * 删除效率测试
     * @param list
     * @return
     */
    public static long deleteTest(List<Integer> list){
        long start = System.currentTimeMillis();
        for(int i=0;i<list.size();i++){
            list.remove(i);
        }
        return System.currentTimeMillis()-start;
    }

    /**
     * 插入效率测试
     * @param list
     * @return
     */
    public static long insertTest(List<Integer> list){
        long start = System.currentTimeMillis();
        int num =  list.size();
        for(int i=0;i<num;i++){
            list.add(i,9);
        }
        return System.currentTimeMillis()-start;
    }
}
